﻿using System;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Oxage.Common;

namespace Oxage.Rasterizer
{
	public partial class Settings
	{
		#region Application
		public static string ApplicationName
		{
			get
			{
				return string.Format("{0} v{1}", ProductName, ProductVersion);
			}
		}

		public static string ProductName
		{
			get
			{
				string result = null;
				Assembly assembly = Assembly.GetExecutingAssembly();
				object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
				if (attributes != null && attributes.Length > 0)
				{
					result = (attributes[0] as AssemblyTitleAttribute).Title;
				}
				return result;
			}
		}

		public static string ProductVersion
		{
			get
			{
				Assembly assembly = Assembly.GetExecutingAssembly();
				Version version = assembly.GetName().Version;
				string result = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build);
				return result;
			}
		}
		#endregion

		#region Registry
		static Settings()
		{
			Config.DefaultCache = false;
			Config.DefaultSource = ConfigSource.Registry;
			Config.RegistryRootPath = @"Software\Oxage\xbraster\";
		}

		public static bool Debug
		{
			get
			{
				bool result;
				return Config.Get("Debug", out result) ? result : false;
			}
			set
			{
				Config.Set("Debug", value);
			}
		}

		public static int JpegQualityLevel
		{
			get
			{
				int result = 0;
				if (!Config.Get("JpegQualityLevel", out result))
				{
					//Default value
					result = 75;
				}
				else if (result < 0)
				{
					//Minimum
					result = 0;
				}
				else if (result > 100)
				{
					//Maximum
					result = 100;
				}
				return result;
			}
			set
			{
				Config.Set("JpegQualityLevel", value);
			}
		}

		public static bool JpegFlipHorizontal
		{
			get
			{
				bool result;
				return Config.Get("JpegFlipHorizontal", out result) ? result : false;
			}
			set
			{
				Config.Set("JpegFlipHorizontal", value);
			}
		}

		public static bool JpegFlipVertical
		{
			get
			{
				bool result;
				return Config.Get("JpegFlipVertical", out result) ? result : false;
			}
			set
			{
				Config.Set("JpegFlipVertical", value);
			}
		}

		public static Rotation ImageRotation
		{
			get
			{
				int value = 0;
				Rotation result = Rotation.Rotate0;
				Config.Get("ImageRotation", out value);
				switch (value)
				{
					default:
					case 0:
						result = Rotation.Rotate0;
						break;

					case 90:
						result = Rotation.Rotate90;
						break;

					case 180:
						result = Rotation.Rotate180;
						break;

					case 270:
						result = Rotation.Rotate270;
						break;
				}
				return result;
			}
			set
			{
				int angle = 0;
				switch (value)
				{
					default:
					case Rotation.Rotate0:
						angle = 0;
						break;

					case Rotation.Rotate90:
						angle = 90;
						break;

					case Rotation.Rotate180:
						angle = 180;
						break;

					case Rotation.Rotate270:
						angle = 270;
						break;
				}
				Config.Set("ImageRotation", angle);
			}
		}

		public static TiffCompressOption TiffCompression
		{
			get
			{
				TiffCompressOption result = TiffCompressOption.Default;
				return Config.Get<TiffCompressOption>("TiffCompression", out result) ? result : TiffCompressOption.Default;
			}
			set
			{
				Config.Set<TiffCompressOption>("TiffCompression", value);
			}
		}

		public static string OutputFormatExtension
		{
			get
			{
				string result;
				return Config.Get("OutputFormatExtension", out result) ? result : ".png";
			}
			set
			{
				Config.Set("OutputFormatExtension", value);
			}
		}

		public static Stretch Stretch
		{
			get
			{
				Stretch result;
				return Config.Get<Stretch>("Stretch", out result) ? result : Stretch.Uniform;
			}
			set
			{
				Config.Set<Stretch>("Stretch", value);
			}
		}

		public static VerticalAlignment VerticalAlignment
		{
			get
			{
				VerticalAlignment result;
				return Config.Get<VerticalAlignment>("VerticalAlignment", out result) ? result : VerticalAlignment.Center;
			}
			set
			{
				Config.Set<VerticalAlignment>("VerticalAlignment", value);
			}
		}

		public static HorizontalAlignment HorizontalAlignment
		{
			get
			{
				HorizontalAlignment result;
				return Config.Get<HorizontalAlignment>("HorizontalAlignment", out result) ? result : HorizontalAlignment.Center;
			}
			set
			{
				Config.Set<HorizontalAlignment>("HorizontalAlignment", value);
			}
		}
		#endregion
	}
}
